﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Revit;
using YJKExcelFormTool.DataDefine;

namespace YJKExcelFormTool.Creator
{
	public class GraphPaperDirectoryCreator : TableCreator
	{
		public GraphPaperDirectoryCreator(ExternalCommandData cmdData)
		{
			this.m_Revit = cmdData;
		}

		public static List<ViewSheet> GetViewSheetList(ExternalCommandData cmdData)
		{
			List<ViewSheet> list = new List<ViewSheet>();
			foreach (Element element in new FilteredElementCollector(cmdData.Application.ActiveUIDocument.Document).OfClass(typeof(ViewSheet)).ToElements())
			{
				ViewSheet viewSheet = element as ViewSheet;
				if (viewSheet != null)
				{
					list.Add(viewSheet);
				}
			}
			list.Sort((ViewSheet sheet1, ViewSheet sheet2) => ViewSheetComparer.Compare(sheet1, sheet2));
			return list;
		}

		public TableBody CreateTableBody(List<TableHeadCellKey> headerParameters, List<ViewSheet> viewSheetList)
		{
			List<TableBodyRow> list = new List<TableBodyRow>();
			if (headerParameters.Count == 0)
			{
				return null;
			}
			int num = 1;
			foreach (ViewSheet viewSheet in viewSheetList)
			{
				if (viewSheet != null)
				{
					List<FamilyInstance> titleBlockListFromViewSheet = this.GetTitleBlockListFromViewSheet(viewSheet);
					if (titleBlockListFromViewSheet.Count >= 1)
					{
						foreach (FamilyInstance titleBlock in titleBlockListFromViewSheet)
						{
							TableBodyRow item = this.CreateSingleDataRecord(num, titleBlock, headerParameters);
							list.Add(item);
							num++;
						}
					}
				}
			}
			return new TableBody(list);
		}

		private TableBodyRow CreateSingleDataRecord(int index, FamilyInstance titleBlock, List<TableHeadCellKey> headerParameters)
		{
			List<TableBodyCell> list = new List<TableBodyCell>();
			foreach (TableHeadCellKey tableHeadCellKey in headerParameters)
			{
				Parameter parameter = titleBlock.GetParameter(tableHeadCellKey.KeyName);
				TableBodyCell item;
				if (parameter == null)
				{
					if (tableHeadCellKey.KeyName.CompareTo("序号") == 0)
					{
						item = new TableBodyCell(TableBodyCell._tagDataType._DT_TEXT, index.ToString());
					}
					else if (tableHeadCellKey.KeyName.CompareTo("图幅") == 0)
					{
						item = new TableBodyCell(TableBodyCell._tagDataType._DT_TEXT, titleBlock.Name);
					}
					else
					{
						item = new TableBodyCell(TableBodyCell._tagDataType._DT_TEXT, "");
					}
				}
				else
				{
					item = new TableBodyCell(TableBodyCell._tagDataType._DT_TEXT, parameter.AsString());
				}
				list.Add(item);
			}
			return new TableBodyRow(list);
		}

		private List<FamilyInstance> GetTitleBlockListFromViewSheet(ViewSheet viewSheet)
		{
			IEnumerable<Element> enumerable = new FilteredElementCollector(this.m_Revit.Application.ActiveUIDocument.Document, viewSheet.Id).OfClass(typeof(FamilyInstance)).OfCategory(BuiltInCategory.OST_TitleBlocks).ToElements();
			List<FamilyInstance> list = new List<FamilyInstance>();
			foreach (Element element in enumerable)
			{
				FamilyInstance familyInstance = element as FamilyInstance;
				if (familyInstance != null)
				{
					list.Add(familyInstance);
				}
			}
			return list;
		}
	}
}
